<?php
namespace app\adminjcb\controller;

use app\common\controller\AdminBaseController;
use app\adminjcb\model\User as UserModel;
use think\Db;

/**
 * 后台首页
 * Class Index
 */
class IndexController extends AdminBaseController {

	protected function _initialize() {
		parent::_initialize();
	}

	/**
	 * 首页
	 * @return mixed
	 */
	public function index() {
		// 总会员数
		$total_member = db('member')->count();
		$this->assign("total_member", $total_member);
		// 总余额数
		$total_money = db('member')->sum('money+frozen_money');
		$this->assign("total_money", $total_money);
		// 总币数
		$total_coin = db('member')->sum('coin+trade_coin');
		$this->assign("total_coin", $total_coin);

		// 委托总数
		$total_order = db('order')->where('status', 1)->count();
		$this->assign("total_order", $total_order);
		// 总成交量
		$total_sum = db('order')->where('status', 2)->sum('num');
		$this->assign("total_sum", $total_sum);
		/**
		 * 交易单数
		 * 委托单数
		 * 交易额
		 *
		 */
		return $this->fetch();
	}

	/**
	 * 更新密码
	 * @param $id
	 * @param $group_id
	 */
	public function update() {
		$user_model = new UserModel();
		if ($this->request->isPost()) {
			$data = $this->request->post();
			if (empty($data['old_pwd'])) {
				$this->error('请输入旧密码');
			}
			if (empty($data['login_pwd']) || empty($data['login_pwd2'])) {
				$this->error('请输入新密码');
			}
			if ($data['login_pwd'] != $data['login_pwd2']) {
				$this->error('二次密码输入不一致');
			}
			$user = $user_model->find(session('admin_id'));
			if (md5_salt($data['old_pwd']) != $user['login_pwd']) {
				$this->error('旧密码错误');
			}
			$user->id = session('admin_id');
			$user->login_pwd = md5_salt($data['login_pwd']);
			if ($user->save() !== false) {
				$this->success('更新成功');
			} else {
				$this->error('更新失败');
			}
		} else {
			$user = $user_model->find(session('admin_id'));
			return $this->fetch('update', array('user' => $user));
		}
	}

}
